//package com.ly.order.config;
//
//import com.ly.order.listener.OrderTransactionListener;
//import org.apache.rocketmq.client.exception.MQClientException;
//import org.apache.rocketmq.client.producer.TransactionMQProducer;
//import org.apache.rocketmq.client.producer.TransactionSendResult;
//import org.apache.rocketmq.common.message.Message;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.PostConstruct;
//import java.util.concurrent.ArrayBlockingQueue;
//import java.util.concurrent.ThreadPoolExecutor;
//import java.util.concurrent.TimeUnit;
//
//
//
////启动事务的生产者
////主要就是创建事务消息的发送者。
//// 在这里，我们重点关注 OrderTransactionListener，它负责执行本地事务和事务状态回查
//@Component
//public class TransactionProducer {
//
//    private String producerGroup = "order_trans_group";
//    private TransactionMQProducer producer;
//
//    //用于执行本地事务和事务状态回查的监听器
//    @Autowired
//    OrderTransactionListener orderTransactionListener;
//    //执行任务的线程池
//    ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 60,
//            TimeUnit.SECONDS, new ArrayBlockingQueue<>(50));
//
//    @PostConstruct
//    public void init(){
//        producer = new TransactionMQProducer(producerGroup);
//        producer.setNamesrvAddr("192.168.23.104:9876;192.168.23.102:9876");
//        producer.setSendMsgTimeout(Integer.MAX_VALUE);
//        producer.setExecutorService(executor);
//        producer.setTransactionListener(orderTransactionListener);
//        this.start();
//    }
//    private void start(){
//        try {
//            this.producer.start();
//        } catch (MQClientException e) {
//            e.printStackTrace();
//        }
//    }
//    //事务消息发送
//    public TransactionSendResult send(String data, String topic){
//        try {
//            Message message = new Message(topic,data.getBytes());
//            return this.producer.sendMessageInTransaction(message, null);
//        }catch (Exception e){
//            e.printStackTrace();
//            return null;
//        }
//    }
//
//}
